import requests
from fake_useragent import UserAgent
import csv

# 设置请求头
headers = {
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'en-US,en;q=0.8',
    'User-Agent': UserAgent().random,
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Referer': 'https://weibo.com/newlogin?',
    'Connection': 'keep-alive',
}

# 发起请求
url = 'https://weibo.com/ajax/side/hotSearch'
response = requests.get(url, headers=headers)

# 检查请求是否成功
if response.status_code == 200:
    try:
        # 尝试将响应内容解析为 JSON
        data_dict = response.json()
    except requests.exceptions.JSONDecodeError:
        print("响应内容不是有效的JSON！")
        print("响应内容前200字符：", response.text[:200])
else:
    print(f"请求失败，状态码：{response.status_code}")
    exit()

# 提取热搜数据
try:
    datas = data_dict["data"]["realtime"]
except KeyError:
    print("JSON 数据结构异常，未找到 'data' 或 'realtime' 字段")
    exit()

# 写入 CSV 文件
with open('weibo_hot_search.csv', 'w', encoding='utf-8-sig', newline='') as f:
    writer = csv.writer(f)

    # 写入表头
    writer.writerow(['Note', 'Num', 'Label', 'Flag'])

    # 遍历并写入每一行数据
    for data in datas:
        note = data.get("note", "")
        num = data.get("num", "")
        label_name = data.get("label_name", "")
        flag_desc = data.get("flag_desc", "")

        writer.writerow([note, num, label_name, flag_desc])

# 可选：控制台输出部分数据以验证
print("数据已成功写入 weibo_hot_search.csv")